package com.rocky.flutter.tuya.tuya;

import android.app.Application;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

import java.lang.reflect.InvocationTargetException;

import io.flutter.plugin.common.MethodChannel;

public class TuyaAccount {

    public static void init() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Application mApplication = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null,(Object[])null);
        TuyaHomeSdk.init(mApplication, "rwtkfrsgnwadx3gd59pk","5hewgdfpaavnas9xdend5fjkt4sggcwp");
    }

    public static void Login(String phone, String password, MethodChannel.Result result){
        //手机密码登录
        TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", phone, password, new ILoginCallback() {
            @Override
            public void onSuccess(User user) {
                String str = JSON.toJSONString(user);
                result.success(str);
            }

            @Override
            public void onError(String code, String error) {
                result.error(code,error, null);
            }
        });
    }

    public static void LoginById(String uid, String password, boolean isCreateHome,MethodChannel.Result result) {
        // UID 登录
        TuyaHomeSdk.getUserInstance().loginOrRegisterWithUid("86", "1234", "123456", new ILoginCallback() {
            @Override
            public void onSuccess(User user) {
                String str = JSON.toJSONString(user);
                result.success(str);
            }

            @Override
            public void onError(String code, String error) {
                result.error(code,error, null);
            }
        });

    }
}
